<?php
defined('BY_JYA') or exit('error');

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'文章列表'),
	array('op'=>'edit','title'=>'添加文章'),
);
if($_RQ['op']=='list'){
	$fenlei=pdo_getall('core_account_article_fenlei',array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_peiedu'),array('id','title'),'id');
	$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_peiedu');
	$fenleiid=intval($_RQ['fenlei']);
	if($fenleiid){
		$params['fenlei']=$fenleiid;
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fields=array('id','paixu','title','fee','ishot','fenlei','pic_url','keyword','createtime','re_readnum','readnum','likenum','status','content');
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$order=intval($_RQ['order']);
	if($order==1){
		$order='id desc';
	}elseif($order==2){
		$order='ishot desc, id desc';
	}else{
		$order='paixu desc, id desc, readnum desc';
	}
	//print_r($params);exit;
	$list=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'',$order);
	if($list){
		foreach($list as &$item){
			$item['content'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['content'],0,36);
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['fenlei']=$fenlei[$item['fenlei']]['title'];
			$item['url']=custom_url('jy_peiedu/newsdetail/id='.$item['id'],true);
		}
	}
	$return['fenleiname']=$fenlei[$fenleiid]['title'];
	$return['list']=$list?$list:array();
	$return['fenlei']=$fenlei?$fenlei:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'paixu','title'=>'排序(点击编辑↓)','type'=>'string','set'=>'set'),
			array('name'=>'title','title'=>'文章标题','type'=>'string'),
			array('name'=>'fenlei','title'=>'文章分类','type'=>'string'),
			array('name'=>'keyword','title'=>'关键字','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			array('title'=>'阅读量/点赞量','type'=>'img_string',
			'data'=>array(
				array('title'=>'真实阅读量：','name'=>'re_readnum'),
				array('title'=>'点赞量：','name'=>'likenum')
				)
			),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'ishot','title'=>'是否推荐','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'title','help'=>'','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择文章！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['paixu'])){
		$update['paixu']=$post['paixu'];
	}
	if(isset($post['ishot'])){
		$update['ishot']=$post['ishot'];
	}
	if(isset($post['status'])){
		$update['status']=$post['status'];
	}
	if($update){
		pdo_update('core_account_article',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_article',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'新闻',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'string','title'=>'标题','name'=>'title', 'help'=>'' ),
			array('type'=>'text','title'=>'描述','name'=>'desc', 'help'=>'' ),
			array('name'=>'fenlei','title'=>'新闻分类','type'=>'select','options'=>'fenleis'),
			array('name'=>'keyword','title'=>'关键字','type'=>'text'),
			array('type'=>'richtext','title'=>'内容','name'=>'content', 'help'=>'' ),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['fenleis']=pdo_getall('core_account_article_fenlei',array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_peiedu'),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'paixu'=>intval($postdata['paixu']),
		'status'=>intval($postdata['status']),
		'title'=>trim($postdata['title']),
		'desc'=>trim($postdata['desc']),
		'keyword'=>trim($postdata['keyword']),
		'fenlei'=>trim($postdata['fenlei']),
		'content'=>trim($postdata['content'])
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_account_article',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('修改成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$data['plugin_name']='jy_peiedu';
		pdo_insert('core_account_article',$data);
		exi('增加成功！','','list');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_article',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_article',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}